package demo.controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import demo.bean.LifeBean;
import demo.bean.PropertyBean;
import demo.service.HelloService;
import inject.Zoo;
import util.AopUtil;
import util.AppEventUtil;
import util.AppEventUtil.ApplicationEventDTO;

@RestController
public class InfoController {
	
	@Value("${server.port}")
	private int port;

	@Value("${app.name}")
	private String appName;
	
	@Autowired
	private HttpServletRequest requestField;
	
	@Autowired
	private Zoo zoo;
	
	@Autowired
	private HelloService helloService;
	
	@Resource(name="helloService2")
	private HelloService helloService2;
	
	@Autowired
	private LifeBean lifeBean;
	
	@Autowired
	private PropertyBean propertyBean;

	
	@RequestMapping("/info")
	public String info(){		
		return appName+":"+port+",realport="+AppEventUtil.getPort()+" "+LocalDateTime.now();
	}
	
	
	@RequestMapping("/helloServiceInfo")
	public Map<String,Object> helloServiceInfo(){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("helloService", helloService.toString());
		map.put("helloService2", helloService2.toString());
		return map;
	}
	

	@RequestMapping("/requestInfo")
	public Map<String,Object> requestInfo(HttpServletRequest requestParam) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
	    HttpServletRequest request = servletRequestAttributes.getRequest();
	    
	    map.put("requestField", requestField.toString());
	    map.put("request", request.toString());
	    map.put("requestParam", requestParam.toString());
	    map.put("requestFieldClass", requestField.getClass());

	    Object proxyTarget = AopUtil.getTarget(requestField);
	    
	    map.put("proxyTargetClass", proxyTarget.getClass());

	    map.put("proxyTarget", proxyTarget.toString());

		
		return map;
	}
	
	
	@RequestMapping("/zooInfo")
	public String zooInfo() throws Exception{
		return zoo.info();
	}
	
	@RequestMapping("/sayHello")
	public String sayHello(HttpServletRequest request) throws Exception{
		String name = request.getParameter("name");
		return helloService.hello(name);
	}
	
	@RequestMapping("/sayHello2")
	public String sayHello2(@RequestParam("name") String name) throws Exception{
		return helloService.hello(name);
	}
	
	@RequestMapping("/sayHello3")
	public String sayHello3(@RequestParam(name="name",required = false,defaultValue = "default") String name) throws Exception{
		return helloService.hello(name);
	}

	@RequestMapping("/getEnv")
	public Environment getEnv(){
		return lifeBean.getEnvironment();
	}
	
	@RequestMapping("/getProperty")
	public String getProperty(String name){
		return lifeBean.getEnvironment().getProperty(name);
	}
	
	@RequestMapping("/getLifeBeanInfos")
	public List<String> getLifeBeanInfos(String name){
		return lifeBean.getInfo();
	}
	
	@RequestMapping("/getPropertyBean")
	public PropertyBean getPropertyBean(){
		return propertyBean;
	}
	
	@RequestMapping("/allAppEvents")
	public List<ApplicationEventDTO> allAppEvents(){
		return AppEventUtil.all();
	}
	
	
	
}